var SPall = document.querySelector('.SPall')
$.ajax({
	type: "get",
	url: "http://jx.xuzhixiang.top/ap/api/cart-list.php",
	async: true,
	data: {
		"id": 15157
	},
	success: function(res) {
		var obj = JSON.parse(res);

		var objs = obj.data
		console.log(objs)

		html = ``
		for(var i = 0; i < objs.length; i++) {

			html = `
				<div class="huowu">
				<input type="checkbox" name="" id="" value="" class="only" />
				<img src="${objs[i].pimg}" alt="" />
				<span><a href="javascript:;">[不可空运]</a> ${objs[i].pname}</span>
				<div class="anniuww">
					<div class="buy-d s0">
						<a href="javascript:;" id="zuobians">
							<i class="zuojian">-</i>
						</a>
						<strong class="duoshage">${objs[i].pnum}</strong>

						<!--我要买点击-->
						<!--我要买点击-->
						<!--我要买点击-->
						<!--我要买点击-->
						<!--我要买点击-->
						<!--我要买点击-->
						<!--我要买点击-->
						<a href="javascript:;" id="youbians">
							<i class="youjian">+</i>
						</a>

					</div>
					<h4>有货</h4>
				</div>
				<div class="jiage jiage2">
					<strong>￥<u class="XZJ">${objs[i].pprice*objs[i].pnum}</u></strong>
					<i>￥<u class="yunajia">${objs[i].pprice}</u>/件</i>
				</div>
				<div class="jiage jiage1 jiage3">
					<strong>￥${objs[i].pprice}</strong>
					<!--<i>￥${objs[i].pprice}/件</i>-->
				</div>
				<div class="SCSC">
					<a href="javascript:;">[收藏]</a>
					<a href="javascript:;" class="shanchu">[删除]</a>
				</div>
			</div>
			`

			SPall.innerHTML += html
			if(objs[i].pnum >= 2) {
				console.log(10000)
				$('.jiage3').eq(i).removeClass('jiage1')
				$('.jiage2').eq(i).addClass('jiage1')
			}
		}

		var All1 = document.querySelector('.All1')
		var All2 = document.querySelector('.All2')
		var only = document.querySelectorAll('.only')
		var XZJ = document.querySelectorAll('.XZJ')
		var YJ = document.querySelectorAll('.yunajia')
		var zongjia = document.querySelector('.zongjia')
		var duoshage = document.querySelectorAll('.duoshage')
		var pice = document.querySelector('.pice')
		All1.onclick = function() {
			//	location.href="gouwuche.html"
			for(var i = 0; i < only.length; i++) {
				only[i].checked = this.checked

			}
			All2.checked = this.checked

			var plussum = 0;
			var plusAllNum = 0;
			for(let i = 0; i < only.length; i++) {

				if(only[i].checked) {
					var s = duoshage[i].innerHTML
					//						console.log(s)
					var c = XZJ[i].innerHTML;
					//						console.log(c)
					plussum += parseFloat(s);
					console.log(plussum)
					var d = XZJ[i].innerHTML;
					plusAllNum += parseFloat(d);
					//					    plussum+=parseFloat(s);
					console.log(plussum)
				} else {
					//						flag1=false
				}
			}
			//				plussum=b.toFixed(2);

			zongjia.innerHTML = parseInt(plusAllNum)
			pice.innerHTML = plussum
		}
		All2.onclick = function() {
			//	location.href="gouwuche.html"
			for(var i = 0; i < only.length; i++) {
				only[i].checked = this.checked

			}
			All1.checked = this.checked

			var plussum = 0;
			var plusAllNum = 0;
			for(let i = 0; i < only.length; i++) {

				if(only[i].checked) {
					var s = duoshage[i].innerHTML
					//						console.log(s)
					var c = XZJ[i].innerHTML;
					//						console.log(c)
					plussum += parseFloat(s);
					console.log(plussum)
					var d = XZJ[i].innerHTML;
					plusAllNum += parseFloat(d);
					//					    plussum+=parseFloat(s);
					console.log(plussum)
				} else {
					//						flag1=false
				}
			}
			//				plussum=b.toFixed(2);

			zongjia.innerHTML = parseInt(plusAllNum)
			pice.innerHTML = plussum
		}

		$('.only').on('click', function() {
			num = $(this).index('.only');
			//	console.log(num)
			console.log(XZJ[num].innerHTML)

			var val = $(".only").eq(num).is(":checked");
			if(val == true) {

			}
			for(var j = 0; j < only.length; j++) {
				var val = $(".only").eq(j).is(":checked");
				if(val == true) {

				}
				if(val == false) {
					//			console.log(1)
					$(".All1").attr("checked", false)
					$(".All2").attr("checked", false)
				} else {
					//			console.log(2)
					All1.checked = true
					All2.checked = true
					//			console.log(j)
					for(var s = 0; s < only.length; s++) {
						if(only[s].checked == false) {
							All1.checked = false
							All2.checked = false
						}
					}

				}
			}
			//总价计算
			var plussum = 0;
			var plusAllNum = 0;
			for(let i = 0; i < only.length; i++) {

				if(only[i].checked) {
					var s = duoshage[i].innerHTML
					//						console.log(s)
					var c = XZJ[i].innerHTML;
					//						console.log(c)
					plussum += parseFloat(s);
					console.log(plussum)
					var d = XZJ[i].innerHTML;
					plusAllNum += parseFloat(d);
					//					    plussum+=parseFloat(s);
					console.log(plussum)
				} else {
					//						flag1=false
				}
			}
			//				plussum=b.toFixed(2);

			zongjia.innerHTML = parseInt(plusAllNum)
			pice.innerHTML = plussum
		})

		$('.buy-d').hover(function() {
			num = $(this).index('.buy-d');
			//	console.log(num)
			//	var XZJ=document.querySelectorAll('.XZJ')

			var nums = document.querySelectorAll('.duoshage')
			var num1 = nums[num].innerHTML
			$('.buy-d #youbians').eq(num).on('click', function() {
//				var num=$(this).index('#youbians')
				num1++
				//		console.log(num1)

				//		console.log(nums[num].innerHTML)
				if(num1 >= 2) {
					//			console.log(18)
					$('.jiage3').eq(num).removeClass('jiage1')
					$('.jiage2').eq(num).addClass('jiage1')
					//			console.log($('.buy-d .jiage'))
				}
				var sum = YJ[num].innerHTML
				XZJ[num].innerHTML = sum * num1
				nums[num].innerHTML = num1
				var plussum = 0;
				var plusAllNum = 0;
				for(let i = 0; i < only.length; i++) {

					if(only[i].checked) {
						var s = duoshage[i].innerHTML
						var c = YJ[i].innerHTML * num1;
						console.log(c)
						plussum += parseFloat(s);
						console.log(plussum)
						var d = XZJ[i].innerHTML;
						//					    var d =XZJ[i].innerHTMWL;
						console.log(d)
						plusAllNum += parseFloat(d);
					} else {
						//						flag1=false
					}
				}
				//				plussum=b.toFixed(2);

				zongjia.innerHTML = parseInt(plusAllNum)
				pice.innerHTML = plussum

				var duoshage = document.querySelectorAll('.duoshage')
				var pid = objs[num].pid
				var uid = objs[num].uid
				var pnum = parseFloat(duoshage[num].innerHTML)
				$.ajax({
					type: "get",
					url: "http://jx.xuzhixiang.top/ap/api/cart-update-num.php",
					async: true,
					data: {
						"uid": uid,
						"pid": pid,
						"pnum": pnum
					},
					success: function() {
						console.log(1)
					}
				});
			})
			$('.buy-d #zuobians').eq(num).on('click', function() {
//				var num=$(this).index('#zuobians')
				num1--
				if(num1 == 1 || num1 == 0) {
					num1 = 1

					$('.jiage2').eq(num).removeClass('jiage1')
					$('.jiage3').eq(num).addClass('jiage1')
				}
				var sum = YJ[num].innerHTML
				XZJ[num].innerHTML = sum * num1
				nums[num].innerHTML = num1
				var plussum = 0;
				var plusAllNum = 0;
				for(let i = 0; i < only.length; i++) {

					if(only[i].checked) {
						var s = duoshage[i].innerHTML
						var c = YJ[i].innerHTML * num1;
						console.log(c)
						plussum += parseFloat(s);
						//					    plussum+=parseFloat(c);
						console.log(plussum)
						var d = XZJ[i].innerHTML;
						//					    var d =XZJ[i].innerHTMWL;
						console.log(d)
						plusAllNum += parseFloat(d);
					} else {
						//						flag1=false
					}
				}
				//				plussum=b.toFixed(2);

				zongjia.innerHTML = parseInt(plusAllNum)
				pice.innerHTML = plussum
				var duoshage = document.querySelectorAll('.duoshage')
				var pid = objs[num].pid
				var uid = objs[num].uid
				var pnum = parseFloat(duoshage[num].innerHTML)
				$.ajax({
					type: "get",
					url: "http://jx.xuzhixiang.top/ap/api/cart-update-num.php",
					async: true,
					data: {
						"uid": uid,
						"pid": pid,
						"pnum": pnum
					},
					success: function() {
						console.log(1)
					}
				});
			})

		})

		//	var delate=document.querySelector('.shanchu')
		$('.shanchu').on('click', function() {
			//		console.log(1)
			num = $(this).index('.shanchu');
			var pid = objs[num].pid
			var uid = objs[num].uid
			console.log(pid)
			console.log(uid)
			$.ajax({
				type: "get",
				url: "http://jx.xuzhixiang.top/ap/api/cart-delete.php",
				async: true,
				data: {
					"uid": uid,
					"pid": pid
				},
				success: function(res) {
					var obj = JSON.parse(res);
					console.log(obj)
					var huowu = document.querySelectorAll('.huowu')
					huowu[num].remove()
				}
			});
		})
	}
});